trigger ValidationRotingPlanAndVisit on Routing_Plan_And_Visit__c (before update,before insert) {
        Boolean Isupdate=false;
        List<Id> idList=new List<Id>();
        List<Date> dateList=new List<Date>();
        for(Integer i=0;i<trigger.new.size();i++)
        {
            Routing_Plan_And_Visit__c va=trigger.new[i];
            if(trigger.isUpdate)
            {
                if(trigger.new[i].Visit_Date__c!=trigger.old[i].Visit_Date__c || trigger.new[i].OwnerId != trigger.old[i].OwnerId)
                {
                    Isupdate=true;
                    idList.add(va.OwnerId);
                    dateList.add(va.Visit_Date__c);
                }
            }
            else
            {
                idList.add(va.OwnerId);
                dateList.add(va.Visit_Date__c);
            }
        }
        if(Isupdate || trigger.isInsert)
        {
            List<Routing_Plan_And_Visit__c> rpList=[select Id,Name,OwnerId,Visit_Date__c from Routing_Plan_And_Visit__c where OwnerId in:idList and Visit_Date__c in:dateList];
            Map<String,Date> mapList=new Map<String,Date>();
            for(Routing_Plan_And_Visit__c r:rpList)
            {
                mapList.put(String.valueOf(r.OwnerId)+String.valueOf(r.Visit_Date__c),r.Visit_Date__c);
            }
            for(Integer i=0;i<trigger.new.size();i++)
            {
                Routing_Plan_And_Visit__c va=trigger.new[i];
                if(mapList.containsKey(String.valueOf(va.OwnerId)+String.valueOf(va.Visit_Date__c)))
                {
                    trigger.new[i].Visit_Date__c.addError('已存在该日的拜访计划');
                }
            }
        }
}